<?php
/*
 * 支付宝相关控制器
 */
class AlipayController extends BasicController {
    
    private function init(){
        //接口响应控制器可以token为空
        Yaf_Registry::get('tokenPlugin')->checkAuth();
    }

    /*
    * 人机英语支付宝回调 Api
    */
    public function ennotifyAction(){
        Yaf_Loader::import(LIB_PATH.'/alipay/alipay.config.php');
        Yaf_Loader::import(LIB_PATH.'/alipay/lib/alipay_notify.class.php');
        
        $alipay_config['notify_url'] = 'http://webapi.jxzhjy.com/default/alipay/ennotify';
        $alipay_config['return_url'] = '';
        
        //计算得出通知验证结果
        $alipayNotify = new AlipayNotify($alipay_config);
        $verify_result = $alipayNotify->verifyNotify();
        
        $isOk = false;
        
        if($verify_result) {//验证成功
            //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
            $out_trade_no = $_POST['out_trade_no'];//商户订单号
            $trade_no = $_POST['trade_no'];//支付宝交易号

            //首先先判断该商户订单号是否已经被处理过，如果已经被处理过则无需再次处理
            $tradeenModel = Helper::load('Trade_en');
            $orderInfo = $tradeenModel->Where(['tm_out_trade_no' => $out_trade_no])->SelectOne();
            
            if(!empty($orderInfo) && is_array($orderInfo) && ($orderInfo['tm_status'] = 1)){
                // 1.更新该订单状态为已付款 更新付款时间
                $uData['tm_trade_no'] = $trade_no;
                $uData['ptime'] = CUR_TIMESTAMP;
                $uData['mtime'] = CUR_TIMESTAMP;
                
                $r = $tradeenModel->Where(['tm_out_trade_no' => $out_trade_no])->UpdateOne($uData);
                if($r){
                    // 2.更新对应激活码为有效激活码
                    $key = $orderInfo['tm_activationkey'];
                    
                    $ActivationkeyModel = Helper::load('Activationkey');
                    $c = $ActivationkeyModel->Where(['a_key' => $key])->UpdateOne(['is_valid' => 1]);
                    
                    // 3.开始激活流程
                    $ActivationkeyUseModel = Helper::load('Activationkey_use');

                    $info = $ActivationkeyModel->Where(['a_key' => $key])->SelectOne();

                    if($info['a_type'] == 1){//一个月
                        $x = 1;
                    }
                    elseif($info['a_type'] == 2){//三个月
                        $x = 3;
                    }
                    elseif($info['a_type'] == 3){//半年
                        $x = 6;
                    }
                    else{//一年
                        $x = 12;
                    }

                    $grade = $orderInfo['tm_grade'];//用户当前的年级编号
                    $endTime = strtotime(' + '.$x.' month', CUR_TIMESTAMP);

                    $uuData['au_key'] = $key;
                    $uuData['au_grade'] = $grade;
                    $uuData['au_stime'] = CUR_TIMESTAMP;
                    $uuData['au_etime'] = $endTime;
                    $uuData['au_user_id'] = $orderInfo['user_id'];
                    $uuData['ctime'] = CUR_TIMESTAMP;
                    $uuData['mtime'] = CUR_TIMESTAMP;
                    $d = $ActivationkeyUseModel->Insert($uuData);
                        
                    if($c && $d){
                        $isOk = true;
                    }
                }
            }
        }
        
        if($isOk){
            echo "success";
        }
        else{
            echo "fail";
        }
    }
    
}